package top.kaoshanji.u1ch11b;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import top.kaoshanji.u1ch11b.model.Payment;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/1 15:42
 */
@SpringBootTest
@AutoConfigureMockMvc
public class PaymentsControllerTest {

    @Autowired
    private MockMvc mockMvc;

    private static WireMockServer wireMockServer;

    @BeforeAll
    static void init() {
        wireMockServer = new WireMockServer(new WireMockConfiguration().port(8800));
        wireMockServer.start();

        WireMock.configureFor("localhost", 8800);
    }

    /**
     * 代码清单test-u1ch11b-01：一个测试接口实例
     * @throws Exception
     */
    @Test
    public void testCreatePaymentOpenFeign() throws Exception {
        Payment request = new Payment();
        request.setAmount(1001);

        ObjectMapper mapper = new ObjectMapper();

        WireMock.stubFor(WireMock.post(WireMock.urlMatching("/createPaymentOpenFeign"))
                .willReturn(WireMock.aResponse()
                        .withBody(mapper.writeValueAsString(request))
                        .withHeader("content-type", MediaType.APPLICATION_JSON_VALUE)
                        .withStatus(HttpStatus.OK.value())));

        mockMvc.perform(MockMvcRequestBuilders.post("/createPaymentOpenFeign")
                .content(mapper.writeValueAsString(request))
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.amount").value(1001));

    }



}
